/**
 * Created by Administrator on 2019/11/26.
 */

export function localStorageGet(key) {
  return localStorage.getItem(key)
}

export function localStorageSet(key, val) {
  if (val == null) {
    localStorage.removeItem(key)
  } else {
    localStorage.setItem(key, val)
  }
}

export function loadScript(url) {
  const scriptTag = document.createElement('script')
  scriptTag.type = 'text/javascript'
  scriptTag.src = url
  const s = document.getElementsByTagName('head')[0]
  s.appendChild(scriptTag)
  scriptTag.addEventListener('load', () => {
    console.log('load ' + url)
  })
}

export function getFileType(name) {
  const fileList = name.split('.')
  return fileList[fileList.length - 1]
}

export function formatLocationType(that, locationType) {
  switch (locationType) {
    case 'latLng':
      return that.$t('latlng')
    case 'baseStation':
      return that.$t('baseStation')
    default:
      return that.$t('unKnow')
  }
}

export function formatRegisterType(that, registerType) {
  switch (registerType) {
    case 'phone':
      return that.$t('phone')
    case 'email':
      return that.$t('email')
    case 'admin':
      return that.$t('manage')
    default:
      return that.$t('unKnow')
  }
}

export function formatterCreatedType(that, type) {
  switch (type) {
    case 0:
      return that.$t('manual')
    case 1:
      return that.$t('auto')
    case 2:
      return that.$t('upperComputer')
  }
}

export function formatLanguage(that, language) {
  switch (language) {
    case 'zh':
      return that.$t('zh')
    case 'en':
      return that.$t('en')
    case 'pt':
      return that.$t('pt')
    default:
      return that.$t('unKnow')
  }
}

export function listLanguage(that) {
  return [
    {
      value: 'zh',
      label: that.$t('zh')
    },
    {
      value: 'en',
      label: that.$t('en')
    },
    {
      value: 'pt',
      label: that.$t('pt')
    }
  ]
}

export function formatDeviceStatus(that, status) {
  switch (status) {
    case 'OFFLINE':
      return that.$t('OFFLINE')
    case 'ONLINE':
      return that.$t('ONLINE')
    case 'DISABLE':
      return that.$t('DISABLE')
    case 'UNACTIVE':
      return that.$t('UNACTIVE')
    default:
      return that.$t('unKnow')
  }
}

export function listSearchType(that) {
  return [
    {
      value: 'explain',
      label: that.$t('explain')
    },
    {
      value: 'param',
      label: that.$t('param')
    },
    {
      value: 'setting',
      label: that.$t('setting')
    }
  ]
}

export function listSearchLogic(that) {
  return [
    {
      value: 'greater',
      label: that.$t('greater')
    },
    {
      value: 'greaterEqual',
      label: that.$t('greater') + that.$t('equal')
    },
    {
      value: 'equal',
      label: that.$t('equal')
    },
    {
      value: 'lessEqual',
      label: that.$t('less') + that.$t('equal')
    },
    {
      value: 'less',
      label: that.$t('less')
    },
    {
      value: 'unequal',
      label: that.$t('unequal')
    },
    {
      value: 'in',
      label: that.$t('in') + ' X,Y,Z'
    }
  ]
}

export function listDeviceStatus(that) {
  return [
    {
      value: null,
      label: that.$t('all')
    },
    {
      value: 'ONLINE',
      label: that.$t('ONLINE')
    },
    {
      value: 'OFFLINE',
      label: that.$t('OFFLINE')
    },
    {
      value: 'DISABLE',
      label: that.$t('DISABLE')
    },
    {
      value: 'UNACTIVE',
      label: that.$t('UNACTIVE')
    }
  ]
}

export function formatExplainType(that, type) {
  const typeList = listExplainType(that)
  for (const item of typeList) {
    if (item.value === type) {
      return item.label
    }
  }
  return that.$t('unKnow')
}

export function listExplainType(that) {
  return [
    {
      value: 'static',
      label: that.$t('static')
    },
    {
      value: 'line',
      label: that.$t('line')
    },
    {
      value: 'string',
      label: that.$t('string')
    },
    {
      value: 'static:multi',
      label: that.$t('multi') + that.$t('static')
    },
    {
      value: 'line:multi',
      label: that.$t('multi') + that.$t('line')
    },
    {
      value: 'string:multi',
      label: that.$t('multi') + that.$t('string')
    }
  ]
}

export function listSettingType(that) {
  return [
    {
      value: 'custom',
      label: that.$t('custom')
    },
    {
      value: 'fixedParam',
      label: that.$t('fixedParam')
    },
    {
      value: 'switch',
      label: that.$t('switch')
    },
    {
      value: 'time',
      label: that.$t('time')
    },
    {
      value: 'timeSlot',
      label: that.$t('timeSlot')
    },
    {
      value: 'noParam',
      label: that.$t('not') + that.$t('param')
    },
    {
      value: 'slider',
      label: that.$t('slider')
    },
    {
      value: 'multiCustom',
      label: that.$t('multiCustom')
    },
    {
      value: 'video',
      label: that.$t('video')
    },
    {
      value: 'customProtocol',
      label: that.$t('customProtocol')
    }
  ]
}

export function formatSettingType(that, type) {
  return that.$t(type)
}

export function formatConnectType(that, type) {
  switch (type) {
    case 'WIFI':
      return 'WIFI'
    case '4G':
      return '4G'
    case '2G':
      return '2G'
    case 'ethernet':
      return that.$t('ethernet')
    default:
      return that.$t('unKnow')
  }
}

export function formatPhoneStatus(that, status) {
  switch (status) {
    case 1:
      return that.$t('toBeActivated')
    case 2:
      return that.$t('silentPeriod')
    case 3:
      return that.$t('testingPeriod')
    case 4:
      return that.$t('normal')
    case 5:
      return that.$t('shutdown')
    case 6:
      return that.$t('cancelledNumber')
    default:
      return that.$t('unKnow')
  }
}

export function listPhoneStatus(that) {
  return [
    {
      value: null,
      label: that.$t('all')
    },
    {
      value: 1,
      label: that.$t('toBeActivated')
    },
    {
      value: 2,
      label: that.$t('silentPeriod')
    },
    {
      value: 3,
      label: that.$t('testingPeriod')
    },
    {
      value: 4,
      label: that.$t('normal')
    },
    {
      value: 5,
      label: that.$t('shutdown')
    },
    {
      value: 6,
      label: that.$t('cancelledNumber')
    }
  ]
}

export function formatTemplateType(that, templateType) {
  switch (templateType) {
    case 'explain':
      return that.$t('explain')
    case 'deviceCore':
      return that.$t('deviceCore')
    case 'setting':
      return that.$t('setting')
    case 'param':
      return that.$t('param')
    case 'series':
      return that.$t('series')
    case 'product':
      return that.$t('product')
    case 'menu':
      return that.$t('menu')
    case 'app':
      return that.$t('app')
    case 'settingParam':
      return that.$t('setting') + that.$t('param')
    case 'productInfo':
      return that.$t('product') + that.$t('info')
    default:
      return that.$t('unKnow')
  }
}

export function listTemplateType(that) {
  return [
    {
      value: null,
      label: that.$t('all')
    },
    {
      value: 'explain',
      label: that.$t('explain')
    },
    {
      value: 'deviceCore',
      label: that.$t('deviceCore')
    },
    {
      value: 'setting',
      label: that.$t('setting')
    },
    {
      value: 'param',
      label: that.$t('param')
    },
    {
      value: 'series',
      label: that.$t('series')
    },
    {
      value: 'product',
      label: that.$t('product')
    },
    {
      value: 'menu',
      label: that.$t('menu')
    },
    {
      value: 'app',
      label: that.$t('app')
    },
    {
      value: 'settingParam',
      label: that.$t('setting') + that.$t('param')
    },
    {
      value: 'productInfo',
      label: that.$t('product') + that.$t('info')
    }
  ]
}

export function listCameraType(that) {
  return [{
    label: '4GCMS',
    value: 'bm'
  }, {
    label: that.$t('none'),
    value: 'none'
  }, {
    label: 'wvpGBS',
    value: 'wvpGBS'
  }, {
    label: 'EasyGBS',
    value: 'easyGBS'
  }, {
    label: 'Hi',
    value: 'hi'
  }, {
    label: '萤石云',
    value: 'yingShiYun'
  }]
}

export function formatCameraType(that, type) {
  for (const item of listCameraType(that)) {
    if (item.value === type) {
      return item.label
    }
  }
  return that.$t('unKnow')
}

export function listDeviceWarnStatus(that) {
  return [
    {
      value: 0,
      label: that.$t('untreated')
    },
    {
      value: 1,
      label: that.$t('misinformation')
    },
    {
      value: 2,
      label: that.$t('processed')
    }
  ]
}

export function formatDeviceWarnStatus(that, status) {
  for (const item of listDeviceWarnStatus(that)) {
    if (item.value === status) {
      return item.label
    }
  }
  return that.$t('unKnow')
}

export function listWorkOrderStatus(that) {
  return [
    {
      value: 0,
      label: that.$t('untreated')
    },
    {
      value: 1,
      label: that.$t('misinformation')
    },
    {
      value: 2,
      label: that.$t('processed')
    }
  ]
}

export function formatDate(that, time) {
  if (time == null || time === 0) {
    return null
  }

  time = new Date(time * 1000)
  const y = time.getFullYear()
  let M = time.getMonth() + 1
  if (M < 10) {
    M = '0' + M
  }
  let d = time.getDate()
  if (d < 10) {
    d = '0' + d
  }
  let h = time.getHours()
  if (h < 10) {
    h = '0' + h
  }
  let m = time.getMinutes()
  if (m < 10) {
    m = '0' + m
  }
  let s = time.getSeconds()
  if (s < 10) {
    s = '0' + s
  }
  return y + '/' + M + '/' + d + ' ' + h + ':' + m + ':' + s

  // time = +time * 1000
  // const d = new Date(time)
  // const now = Date.now()
  // const diff = (now - d) / 1000
  // if (diff < 30) {
  //     return '刚刚'
  // } else if (diff < 3600) {
  //     // less 1 hour
  //     return Math.ceil(diff / 60) + '分钟前'
  // } else if (diff < 3600 * 24) {
  //     return Math.ceil(diff / 3600) + '小时前'
  // } else if (diff < 3600 * 24 * 2) {
  //     return '1天前'
  // }
  // if (option) {
  //     return parseTime(time, option)
  // } else {
  //     return (
  //         d.getMonth() +
  //         1 +
  //         '月' +
  //         d.getDate() +
  //         '日' +
  //         d.getHours() +
  //         '时' +
  //         d.getMinutes() +
  //         '分'
  //     )
  // }
}

export function formatDeviceSetting(setting) {
  setting.valueText = setting.value
  const paramList = setting.paramList
  if (paramList != null && paramList.length !== 0) {
    for (let j = 0; j < paramList.length; j++) {
      paramList[j].settingId = setting.settingId
      paramList[j].keyName = setting.keyName
      paramList[j].port = setting.port
      if (setting.value === paramList[j].value) {
        setting.valueText = paramList[j].name
      }
    }
    setting.paramList = paramList
  }

  if (setting.type === 'slider') {
    setting.value = parseInt(setting.value)
  } else if (setting.type === 'multiCustom') {
    const valueList = []
    const valueSplitList = setting.value.split(',')

    for (let i = 0, j = valueSplitList.length; i < j; i++) {
      let typeRule = 'custom'
      if (setting.typeRuleList && setting.typeRuleList[i]) {
        typeRule = setting.typeRuleList[i]
      }
      let value = valueSplitList[i]
      if (typeRule === 'slider') {
        value = parseInt(value)
      }
      const item = {
        value: value,
        type: typeRule
      }
      valueList.push(item)
    }
    setting.valueList = valueList
  }
  return setting
}

export function formatDeviceSettingStatus(that, status) {
  if (status === 0) {
    return that.$t('normal')
  } else if (status === 1) {
    return that.$t('device') + that.$t('worry')
  } else if (status === 2) {
    return that.$t('outTime')
  }
}

export function formatDeviceSettingTag(that, tag) {
  if (tag === 'v1') {
    return 'API'
  } else if (tag === 'uni') {
    return that.$t('phone')
  } else if (tag === 'manage') {
    return that.$t('manage')
  } else if (tag === 'wxApp') {
    return that.$t('wxApp')
  } else if (tag === 'linkage') {
    return that.$t('linkage')
  } else {
    return that.$t('unKnow')
  }
}

export function formatSensorType(that, type) {
  switch (type) {
    case 0:
      return that.$t('fromTheMachine')
    case 1:
      return that.$t('sensor')
    case 2:
      return that.$t('host')
    default:
      return that.$t('unKnow')
  }
}

export function listSensorType(that) {
  return [
    {
      value: 0,
      label: that.$t('fromTheMachine')
    },
    {
      value: 1,
      label: that.$t('sensor')
    },
    {
      value: 2,
      label: that.$t('host')
    }
  ]
}

export function formatNoticeType(that, type) {
  return that.$t(type)
}

export function formatTime(that, time) {
  if (time == null || time === 0) {
    return null
  }

  time = new Date(time * 1000)

  let M = time.getMonth() + 1
  if (M < 10) {
    M = '0' + M
  }
  let d = time.getDate()
  if (d < 10) {
    d = '0' + d
  }
  let h = time.getHours()
  if (h < 10) {
    h = '0' + h
  }
  let m = time.getMinutes()
  if (m < 10) {
    m = '0' + m
  }
  let s = time.getSeconds()
  if (s < 10) {
    s = '0' + s
  }
  return h + ':' + m + ':' + s
  // const y = time.getFullYear()
  // time = +time * 1000
  // const d = new Date(time)
  // const now = Date.now()
  // const diff = (now - d) / 1000
  // if (diff < 30) {
  //     return '刚刚'
  // } else if (diff < 3600) {
  //     // less 1 hour
  //     return Math.ceil(diff / 60) + '分钟前'
  // } else if (diff < 3600 * 24) {
  //     return Math.ceil(diff / 3600) + '小时前'
  // } else if (diff < 3600 * 24 * 2) {
  //     return '1天前'
  // }
  // if (option) {
  //     return parseTime(time, option)
  // } else {
  //     return (
  //         d.getMonth() +
  //         1 +
  //         '月' +
  //         d.getDate() +
  //         '日' +
  //         d.getHours() +
  //         '时' +
  //         d.getMinutes() +
  //         '分'
  //     )
  // }
}

export function failureNotice(that, msg, duration = 3000) {
  if (msg == null) {
    msg = that.$t('failure')
  }
  that.$notify({
    title: that.$t('failure'),
    message: msg,
    duration: duration,
    type: 'warning'
  })
}

export function failureMessage(that, msg, duration = 3000) {
  if (msg == null) {
    msg = that.$t('failure')
  }
  that.$message({
    message: msg,
    type: 'warning',
    duration: duration
  })
}

export function successNotice(that, msg) {
  if (msg == null) {
    msg = that.$t('success')
  }
  that.$notify({
    title: that.$t('success'),
    message: msg,
    duration: 3000,
    type: 'success'
  })
}

export function successMessage(that, msg) {
  if (msg == null) {
    msg = that.$t('success')
  }
  that.$message({
    message: msg,
    type: 'success'
  })
}

export function cancelNotice(that) {
  that.$notify.info({
    title: that.$t('cancel'),
    duration: 3000,
    message: that.$t('cancel') + that.$t('success')
  })
}

export function requestFullScreen(element) {
  const requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen
  if (requestMethod) {
    requestMethod.call(element)
  } else if (typeof window.ActiveXObject !== 'undefined') {
    const wscript = new ActiveXObject('WScript.Shell')
    if (wscript !== null) {
      wscript.SendKeys('{F11}')
    }
  }
}

export function exitFullscreen() {
  if (document.exitFullscreen) {
    document.exitFullscreen()
  } else if (document.msExitFullscreen) {
    document.msExitFullscreen()
  } else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen()
  } else if (document.webkitExitFullscreen) {
    document.webkitExitFullscreen()
  }
}

export function listGeoHash() {
  return [
    {
      value: 1,
      label: '2500km'
    },
    {
      value: 2,
      label: '630km'
    },
    {
      value: 3,
      label: '78km'
    },
    {
      value: 4,
      label: '30km'
    },
    {
      value: 5,
      label: '2.4km'
    },
    {
      value: 6,
      label: '610m'
    },
    {
      value: 7,
      label: '76m'
    },
    {
      value: 8,
      label: '19m'
    }
  ]
}

export function listGzhTemplateType(that) {
  return [
    {
      value: 'deviceLowBattery',
      label: that.$t('gzh') + that.$t('msg') + that.$t('device') + that.$t('surplus') + that.$t('template') + that.$t('id')
    },
    {
      value: 'deviceStatus',
      label: that.$t('gzh') + that.$t('msg') + that.$t('device') + that.$t('status') + that.$t('template') + that.$t('id')
    },
    {
      value: 'deviceWarn',
      label: that.$t('gzh') + that.$t('msg') + that.$t('device') + that.$t('warn') + that.$t('template') + that.$t('id')
    },
    {
      value: 'deviceBelong',
      label: that.$t('gzh') + that.$t('msg') + that.$t('bind') + that.$t('device') + that.$t('template') + that.$t('id')
    },
    {
      value: 'deviceUnBelong',
      label: that.$t('gzh') + that.$t('msg') + that.$t('unbind') + that.$t('device') + that.$t('template') + that.$t('id')
    },
    {
      value: 'userLogin',
      label: that.$t('gzh') + that.$t('msg') + that.$t('login') + that.$t('template') + that.$t('id')
    }

  ]
}

export function listLogType(that) {
  return [
    {
      value: null,
      label: that.$t('all')
    },
    {
      value: 'debug',
      label: that.$t('debug')
    },
    {
      value: 'info',
      label: that.$t('info')
    },
    {
      value: 'warn',
      label: that.$t('warn')
    },
    {
      value: 'err',
      label: that.$t('error')
    }
  ]
}

export function listLoginType(that) {
  return [{
    label: that.$t('gzh'),
    value: 'gzh'
  }, {
    label: that.$t('wxApp'),
    value: 'wxApp'
  }, {
    label: that.$t('user') + that.$t('name'),
    value: 'userName'
  }, {
    label: that.$t('apiKey'),
    value: 'apiKey'
  }, {
    label: that.$t('qrCode'),
    value: 'qrcode'
  }, {
    label: that.$t('app'),
    value: 'app'
  }
  ]
}

export function formatterLoginType(that, type) {
  for (const item of listLoginType(that)) {
    if (item.value === type) {
      return item.label
    }
  }
  return type
}

export function listExtendTableColumnType(that) {
  return [{
    value: 'int',
    label: that.$t('number')
  }, {
    value: 'varchar(255)',
    label: that.$t('string')
  }]
}
